﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GtsServer.Common;
using GtsServer.Model;

namespace GtsServer.DAL
{
    /// <summary>
    /// 数据访问类:Notice
    /// </summary>
    public partial class NoticeDAL
    {
        public NoticeDAL()
        { }
        #region  Method

        /// <summary>
        /// 手动添加用户公告
        /// </summary>
        /// <param name="dynameicType"></param>
        /// <param name="userAccount"></param>
        /// <param name="prizeName"></param>
        /// <param name="prizeNum"></param>
        /// <param name="prizePrice"></param>
        /// <returns></returns>
        public bool AddHandNotice(int dynameicType, string userAccount, string prizeName, int prizeNum, int prizePrice)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@dynameicType",dynameicType),
                new SqlParameter("@userAccount",userAccount),
                new SqlParameter("@prizeName",prizeName),
                new SqlParameter("@prizeNum",prizeNum),
                new SqlParameter("@prizePrice",prizePrice)
            };
            try
            {
                DbHelperSQL.RunProcedure("CreateNoticeHand", parms, "CreateNoticeHand");
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "Notice");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Notice");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Notice(");
            strSql.Append("Name,CreateTime,StartTime,EndTime,NoticeContent,State,TypeID,R,G,B,IsFullScre,IsStick,IsRoll,TaskID)");
            strSql.Append(" values (");
            strSql.Append("@Name,@CreateTime,@StartTime,@EndTime,@NoticeContent,@State,@TypeID,@R,@G,@B,@IsFullScre,@IsStick,@IsRoll,@TaskID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@NoticeContent", SqlDbType.VarChar,500),
                    new SqlParameter("@State", SqlDbType.Int,3),
                    new SqlParameter("@TypeID", SqlDbType.Int,3),
                    new SqlParameter("@R", SqlDbType.Int,3),
                    new SqlParameter("@G", SqlDbType.Int,3),
                    new SqlParameter("@B", SqlDbType.Int,3),
                    new SqlParameter("@IsFullScre", SqlDbType.Bit,1),
                    new SqlParameter("@IsStick", SqlDbType.Bit,1),
                    new SqlParameter("@IsRoll", SqlDbType.Bit,1),
                    new SqlParameter("@TaskID",SqlDbType.Int,3)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.CreateTime;
            parameters[2].Value = model.StartTime;
            parameters[3].Value = model.EndTime;
            parameters[4].Value = model.NoticeContent;
            parameters[5].Value = model.State;
            parameters[6].Value = model.TypeID;
            parameters[7].Value = model.R;
            parameters[8].Value = model.G;
            parameters[9].Value = model.B;
            parameters[10].Value = model.IsFullScre;
            parameters[11].Value = model.IsStick;
            parameters[12].Value = model.IsRoll;
            parameters[13].Value = model.TaskID;


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GtsServer.Model.Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Notice set ");
            strSql.Append("Name=@Name,");
            //strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("NoticeContent=@NoticeContent,");
            strSql.Append("State=@State");
            //strSql.Append("TypeID=@TypeID");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
                    //new SqlParameter("@CreateTime", SqlDbType.Date,3),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@NoticeContent", SqlDbType.VarChar,5000),
					new SqlParameter("@State", SqlDbType.Int,4),
                    //new SqlParameter("@TypeID", SqlDbType.Int,4),
                    new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            //parameters[1].Value = model.CreateTime;
            parameters[1].Value = model.StartTime;
            parameters[2].Value = model.EndTime;
            parameters[3].Value = model.NoticeContent;
            parameters[4].Value = model.State;
            //parameters[5].Value = model.TypeID;
            parameters[5].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更行经典模式公告
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateClass(GtsServer.Model.Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Notice set ");
            strSql.Append("Name=@Name,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("NoticeContent=@NoticeContent,");
            strSql.Append("State=@State,");
            strSql.Append("TypeID=@TypeID,");
            strSql.Append("R=@R,");
            strSql.Append("G=@G,");
            strSql.Append("B=@B");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@NoticeContent", SqlDbType.VarChar,5000),
					new SqlParameter("@State", SqlDbType.Int,4),
                    new SqlParameter("@TypeID", SqlDbType.Int,4),
                    new SqlParameter("@ID", SqlDbType.Int,4),
                    new SqlParameter("@R", SqlDbType.Int,4),
                    new SqlParameter("@G", SqlDbType.Int,4),
                    new SqlParameter("@B", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.StartTime;
            parameters[2].Value = model.EndTime;
            parameters[3].Value = model.NoticeContent;
            parameters[4].Value = model.State;
            parameters[5].Value = model.TypeID;
            parameters[6].Value = model.ID;
            parameters[7].Value = model.R;
            parameters[8].Value = model.G;
            parameters[9].Value = model.B;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Notice ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Notice ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GtsServer.Model.Notice GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Name,CreateTime,StartTime,EndTime,TypeID from Notice ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            GtsServer.Model.Notice model = new GtsServer.Model.Notice();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CreateTime"] != null && ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StartTime"] != null && ds.Tables[0].Rows[0]["StartTime"].ToString() != "")
                {
                    model.StartTime = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndTime"] != null && ds.Tables[0].Rows[0]["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeID"] != null && ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    model.TypeID = int.Parse(ds.Tables[0].Rows[0]["TypeID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Name,CreateTime,StartTime,EndTime,TypeID,case TypeID when '1' then '全屏公告' when '2' then '比赛公告' when '3' then '经典模式公告' when '4' then '彩池公告' end TypeName ,NoticeContent,State,case State when '1' then '启用' when '0' then '禁用' end StateName,R,G,B ");
            strSql.Append(" FROM Notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 根据任务id获取启用的公告 任务id为0 为经典模式的公告及彩池公告
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public DataSet GetListByTaskIDAndUsed(int taskID, int typeID)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@taskID",taskID),
                new SqlParameter("@typeID",typeID)
            };
            DataSet ds = DbHelperSQL.RunProcedure("GetNoticeList", parms, "GetNoticeList");
            return ds;

            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("select ID,Name,CreateTime,StartTime,EndTime,TypeID,NoticeContent,State,R,G,B ");
            //strSql.Append(" FROM Notice where StartTime<='" + DateTime.Now + "' and EndTime >='" + DateTime.Now + "' and TaskID=" + taskID + " and typeID=" + typeID + " and state=" + Convert.ToInt32(EnumNoticeState.启用));

            //if (EnumNoticeType.经典模式公告.Equals(taskID))
            //    strSql.Append(" and AutoNoticeType in (" + Convert.ToInt32(EnumDynameicType.充值公告) + "," + Convert.ToInt32(EnumDynameicType.兑奖公告) + "," + Convert.ToInt32(EnumDynameicType.经典彩蛋公告) + "," + Convert.ToInt32(EnumDynameicType.比赛名次公告) + ")");
            //else if (EnumNoticeType.彩池公告.Equals(taskID))
            //    strSql.Append(" and AutoNoticeType in (" + Convert.ToInt32(EnumDynameicType.充值公告) + "," + Convert.ToInt32(EnumDynameicType.兑奖公告) + "," + Convert.ToInt32(EnumDynameicType.彩池彩蛋公告) + "," + Convert.ToInt32(EnumDynameicType.比赛名次公告) + ")");
            //else
            //    strSql.Append(" and AutoNoticeType in (" + Convert.ToInt32(EnumDynameicType.充值公告) + "," + Convert.ToInt32(EnumDynameicType.兑奖公告) + "," + Convert.ToInt32(EnumDynameicType.比赛名次公告) + ")");
            //return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表(比赛公告获取ID)
        /// </summary>
        public DataSet GetListByTaskID(int taskID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Name,CreateTime,StartTime,EndTime,TypeID,NoticeContent,State,R,G,B ");
            strSql.Append(" FROM Notice where TaskID=" + taskID);
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetListReturn(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 ID,Name,CreateTime,StartTime,EndTime,TypeID,NoticeContent,State ");
            strSql.Append(" FROM Notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,Name,CreateTime,StartTime,EndTime,TypeID ");
            strSql.Append(" FROM Notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Notice";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

